﻿using System;
using System.Windows.Media;
using Silvery;
using Silvery.Role.Simple;
using System.Collections.Generic;
namespace Sample02_Role
{
    public enum MonsterState
    {
        None,站立,跑,死亡,挨打,攻击
    }
    //直接从Test5例子里继承过来逻辑
    public class Sample_6 : Sample_5
    {
        
        public Sample_6()
        {
            SimpleRole_Monster moster = new SimpleRole_Monster();
            this.AddBaseObj(moster);
            monsters.Add(moster);
            
        }
        CallTimer createtimer = new CallTimer();//多长时间创建一个角色
        CallTimer attacktimer = new CallTimer();//攻击间隔
        int maxCount = 15;
        List<SimpleRole_Monster> monsters = new List<SimpleRole_Monster>();
        //具体逻辑实现
        public override void OnUpdata(BaseObject sender, ITimer timer)
        {
            if (createtimer.LastCallTimeSpan >= TimeSpan.FromSeconds(2) && monsters.Count < maxCount)
            {
                SimpleRole_Monster moster = new SimpleRole_Monster();
                this.AddBaseObj(moster);
                monsters.Add(moster);
                createtimer.Call();
            }
            for (int i = monsters.Count - 1; i >=0; i--)            
            {
                var monster = monsters[i];
                if (monster != null)
                {
                    switch (monster.MonsterState)
                    {
                        case MonsterState.None:
                            monster.MonsterState = MonsterState.跑;
                            break;
                        case MonsterState.站立:
                            monster.MonsterState = MonsterState.跑;
                            break;
                        case MonsterState.跑:
                            if (monster.X - hero.X < 0)
                                monster.RoleDir = RoleDir.右;
                            else
                                monster.RoleDir = RoleDir.左;

                            if (Math.Abs(monster.X - hero.X) <= 20)
                                monster.MonsterState = MonsterState.攻击;
                            else
                                monster.X += monster.RoleDir == RoleDir.右 ? 3 : -3;

                            break;
                        case MonsterState.死亡:

                            break;
                        case MonsterState.挨打:
                            var healbar = monster.Parts["血条"] as HealthBar;
                            healbar.HealthValue -= 10;
                            if (healbar.HealthValue <= 0)
                            {
                                monster.AnimatMode = EmAnimatMode.Once;
                                monster.MonsterState = MonsterState.死亡;
                                monsters.Remove(monster);
                            }
                            else
                                monster.MonsterState = MonsterState.跑;
                            break;
                        case MonsterState.攻击:
                            if (Math.Abs(monster.X - hero.X) > 20)
                                monster.MonsterState = MonsterState.跑;
                            break;
                        default:
                            break;
                    }                    
                }
            }

            if (hero.IsAttack && attacktimer.LastCallTimeSpan > TimeSpan.FromMilliseconds(100))
            {
                attacktimer.Call();
                var monster = GetNearMonster();
                if(monster != null)
                    monster.MonsterState = MonsterState.挨打;
            }
            base.OnUpdata(sender, timer);
        }
        //找到一个最近的怪物
        SimpleRole_Monster GetNearMonster()
        {
            SimpleRole_Monster s =null;
            foreach (var monster in monsters)
            {
                   
                if (hero.RoleDir == RoleDir.左)
                {
                    if (monster.RoleDir == RoleDir.左)
                        continue;
                }
                else
                    if (hero.RoleDir == RoleDir.右)
                    {
                        if (monster.RoleDir == RoleDir.右)
                            continue;
                    }
                if (s == null)
                {
                    s = monster;
                    continue;
                }
                if (Math.Abs(hero.X - monster.X) < Math.Abs(hero.X - s.X))
                    s = monster;
            }
            return s;
        }
        //这是一个血条
        public class HealthBar : BaseObject
        {
            public static void Create(SimpleRole target, double max, double value)
            {
                target.Parts.Add("血条", new HealthBar() { MaxHealthValue = max, HealthValue = value });
            }
            public HealthBar()
            {
                Width = 60;
                Height = 8;
                Background = new SolidColorBrush(Colors.Red);
                X = -60 / 2;
                Y = -80;
            }
            public double MaxHealthValue { get; set; }
            double _healthvalue = 0;
            public double HealthValue
            {
                get { return _healthvalue; }
                set
                {
                    if (value <= 0)
                        _healthvalue = 0;
                    else
                        _healthvalue = value;
                    Width = (_healthvalue / MaxHealthValue) * 60;
                    
                }
            } 
        }
        public class SimpleRole_Monster : SimpleRole
        {
            Random random = new Random((int)DateTime.Now.Ticks);
            public SimpleRole_Monster()
                : base(new Uri("download/Sample_Role/Monster01.zip", UriKind.Relative))
            {
                RoleName = "怪物";
                HealthBar.Create(this, 100, 100);
                base.BeginDownload();
            }
            
            protected override void OnBodyPackLoaded()
            {
                MonsterState = MonsterState.站立;
                var i = random.Next() % 2 + 1;
                RoleDir = (RoleDir)i;
                if (RoleDir == RoleDir.右)
                    X = 40;
                else
                    X = 500;
                Y = 200;
                base.OnBodyPackLoaded();
            }
            MonsterState mosterstate;
            public MonsterState MonsterState
            {
                get { return mosterstate; }
                set
                {
                    if (mosterstate == value)
                        return;
                    mosterstate = value; 
                    CurrentAnimatName = value.ToString();
                }
            }
            RoleDir roledir;
            public RoleDir RoleDir
            {
                get { return roledir; }
                set
                {
                    if (roledir == value)
                        return;
                    if (BodyPart == null)
                        return;
                    roledir = value;
                    if (value == RoleDir.右)
                        IsMirror = true;
                    else
                        IsMirror = false;
                }
            }
            
        }
    }
}
